Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
LevelUP is a Node.js library that provides a simple interface for interacting with LevelDB, a fast key-value storage library. It allows for efficient storage and retrieval of data, making it suitable for applications that require high-performance data operations.
Basic Put and Get
This feature allows you to store and retrieve key-value pairs in the database. The `put` method is used to store a value with a specific key, and the `get` method is used to retrieve the value associated with a key.
const level = require('level');
const db = level('./mydb');
// Put a key-value pair
await db.put('name', 'LevelUP');
// Get the value for a key
const value = await db.get('name');
console.log(value); // 'LevelUP'
Batch Operations
Batch operations allow you to perform multiple put and delete operations in a single atomic action. This is useful for making multiple changes to the database efficiently.
const level = require('level');
const db = level('./mydb');
// Perform batch operations
await db.batch()
.put('name', 'LevelUP')
.put('type', 'database')
.del('oldKey')
.write();
Streams
Streams provide a way to read and write data in a continuous flow. The `createReadStream` method allows you to read all key-value pairs in the database as a stream, which is useful for processing large datasets.
const level = require('level');
const db = level('./mydb');
// Create a read stream
const stream = db.createReadStream();
stream.on('data', ({ key, value }) => {
console.log(`${key} = ${value}`);
});
Sublevel
Sublevel allows you to create isolated sub-databases within a LevelDB instance. This is useful for organizing data into different namespaces.
const level = require('level');
const sublevel = require('subleveldown');
const db = level('./mydb');
const subdb = sublevel(db, 'sub');
// Put and get in sublevel
await subdb.put('name', 'SubLevelUP');
const value = await subdb.get('name');
console.log(value); // 'SubLevelUP'
LevelDOWN is a lower-level binding for LevelDB, providing a more direct interface to the LevelDB library. It is used as a backend for LevelUP but can be used independently for more fine-grained control over LevelDB operations.
RocksDB is a high-performance key-value store developed by Facebook. It is similar to LevelDB but offers additional features like column families and more tunable performance options. It can be used as an alternative to LevelDB for applications requiring higher performance.
Redis is an in-memory key-value store known for its speed and support for various data structures like strings, hashes, lists, sets, and more. Unlike LevelDB, Redis operates entirely in memory, making it suitable for use cases where low-latency access is critical.
Fast and simple storage. A Node.js wrapper for abstract-leveldown
compliant stores, which follow the characteristics of LevelDB.
LevelDB is a simple key-value store built by Google. It's used in Google Chrome and many other products. LevelDB supports arbitrary byte arrays as both keys and values, singular get, put and delete operations, batched put and delete, bi-directional iterators and simple compression using the very fast Snappy algorithm.
LevelDB stores entries sorted lexicographically by keys. This makes the streaming interface of levelup
- which exposes LevelDB iterators as Readable Streams - a very powerful query mechanism.
The most common store is leveldown
which provides a pure C++ binding to LevelDB. Many alternative stores are available such as level.js
in the browser or memdown
for an in-memory store. They typically support strings and Buffers for both keys and values. For a richer set of data types you can wrap the store with encoding-down
.
The level
package is the recommended way to get started. It conveniently bundles levelup
, leveldown
and encoding-down
. Its main export is levelup
- i.e. you can do var db = require('level')
.
We aim to support Active LTS and Current Node.js releases as well as browsers. For support of the underlying store, please see the respective documentation.
If you are upgrading: please see UPGRADING.md
.
First you need to install levelup
! No stores are included so you must also install leveldown
(for example).
$ npm install levelup leveldown
All operations are asynchronous. If you do not provide a callback, a Promise is returned.
var levelup = require('levelup')
var leveldown = require('leveldown')
// 1) Create our store
var db = levelup(leveldown('./mydb'))
// 2) Put a key & value
db.put('name', 'levelup', function (err) {
if (err) return console.log('Ooops!', err) // some kind of I/O error
// 3) Fetch by key
db.get('name', function (err, value) {
if (err) return console.log('Ooops!', err) // likely the key was not found
// Ta da!
console.log('name=' + value)
})
})
levelup()
db.open()
db.close()
db.put()
db.get()
db.del()
db.batch()
(array form)db.batch()
(chained form)db.isOpen()
db.isClosed()
db.createReadStream()
db.createKeyStream()
db.createValueStream()
db.iterator()
levelup(db[, options[, callback]])
The main entry point for creating a new levelup
instance.
db
must be an abstract-leveldown
compliant store.options
is passed on to the underlying store when opened and is specific to the type of store being usedCalling levelup(db)
will also open the underlying store. This is an asynchronous operation which will trigger your callback if you provide one. The callback should take the form function (err, db) {}
where db
is the levelup
instance. If you don't provide a callback, any read & write operations are simply queued internally until the store is fully opened.
This leads to two alternative ways of managing a levelup
instance:
levelup(leveldown(location), options, function (err, db) {
if (err) throw err
db.get('foo', function (err, value) {
if (err) return console.log('foo does not exist')
console.log('got foo =', value)
})
})
Versus the equivalent:
// Will throw if an error occurs
var db = levelup(leveldown(location), options)
db.get('foo', function (err, value) {
if (err) return console.log('foo does not exist')
console.log('got foo =', value)
})
db.open([callback])
Opens the underlying store. In general you should never need to call this method directly as it's automatically called by levelup()
.
However, it is possible to reopen the store after it has been closed with close()
, although this is not generally advised.
If no callback is passed, a promise is returned.
db.close([callback])
close()
closes the underlying store. The callback will receive any error encountered during closing as the first argument.
You should always clean up your levelup
instance by calling close()
when you no longer need it to free up resources. A store cannot be opened by multiple instances of levelup
simultaneously.
If no callback is passed, a promise is returned.
db.put(key, value[, options][, callback])
put()
is the primary method for inserting data into the store. Both key
and value
can be of any type as far as levelup
is concerned.
options
is passed on to the underlying store.
If no callback is passed, a promise is returned.
db.get(key[, options][, callback])
get()
is the primary method for fetching data from the store. The key
can be of any type. If it doesn't exist in the store then the callback or promise will receive an error. A not-found err object will be of type 'NotFoundError'
so you can err.type == 'NotFoundError'
or you can perform a truthy test on the property err.notFound
.
db.get('foo', function (err, value) {
if (err) {
if (err.notFound) {
// handle a 'NotFoundError' here
return
}
// I/O or other error, pass it up the callback chain
return callback(err)
}
// .. handle `value` here
})
options
is passed on to the underlying store.
If no callback is passed, a promise is returned.
db.del(key[, options][, callback])
del()
is the primary method for removing data from the store.
db.del('foo', function (err) {
if (err)
// handle I/O or other error
});
options
is passed on to the underlying store.
If no callback is passed, a promise is returned.
db.batch(array[, options][, callback])
(array form)batch()
can be used for very fast bulk-write operations (both put and delete). The array
argument should contain a list of operations to be executed sequentially, although as a whole they are performed as an atomic operation inside the underlying store.
Each operation is contained in an object having the following properties: type
, key
, value
, where the type is either 'put'
or 'del'
. In the case of 'del'
the value
property is ignored. Any entries with a key
of null
or undefined
will cause an error to be returned on the callback
and any type: 'put'
entry with a value
of null
or undefined
will return an error.
var ops = [
{ type: 'del', key: 'father' },
{ type: 'put', key: 'name', value: 'Yuri Irsenovich Kim' },
{ type: 'put', key: 'dob', value: '16 February 1941' },
{ type: 'put', key: 'spouse', value: 'Kim Young-sook' },
{ type: 'put', key: 'occupation', value: 'Clown' }
]
db.batch(ops, function (err) {
if (err) return console.log('Ooops!', err)
console.log('Great success dear leader!')
})
options
is passed on to the underlying store.
If no callback is passed, a promise is returned.
db.batch()
(chained form)batch()
, when called with no arguments will return a Batch
object which can be used to build, and eventually commit, an atomic batch operation. Depending on how it's used, it is possible to obtain greater performance when using the chained form of batch()
over the array form.
db.batch()
.del('father')
.put('name', 'Yuri Irsenovich Kim')
.put('dob', '16 February 1941')
.put('spouse', 'Kim Young-sook')
.put('occupation', 'Clown')
.write(function () { console.log('Done!') })
batch.put(key, value)
Queue a put operation on the current batch, not committed until a write()
is called on the batch.
This method may throw
a WriteError
if there is a problem with your put (such as the value
being null
or undefined
).
batch.del(key)
Queue a del operation on the current batch, not committed until a write()
is called on the batch.
This method may throw
a WriteError
if there is a problem with your delete.
batch.clear()
Clear all queued operations on the current batch, any previous operations will be discarded.
batch.length
The number of queued operations on the current batch.
batch.write([options][, callback])
Commit the queued operations for this batch. All operations not cleared will be written to the underlying store atomically, that is, they will either all succeed or fail with no partial commits.
The optional options
object is passed to the .write()
operation of the underlying batch object.
If no callback is passed, a promise is returned.
db.isOpen()
A levelup
instance can be in one of the following states:
isOpen()
will return true
only when the state is "open".
db.isClosed()
See isOpen()
isClosed()
will return true
only when the state is "closing" or "closed", it can be useful for determining if read and write operations are permissible.
db.createReadStream([options])
Returns a Readable Stream of key-value pairs. A pair is an object with key
and value
properties. By default it will stream all entries in the underlying store from start to end. Use the options described below to control the range, direction and results.
db.createReadStream()
.on('data', function (data) {
console.log(data.key, '=', data.value)
})
.on('error', function (err) {
console.log('Oh my!', err)
})
.on('close', function () {
console.log('Stream closed')
})
.on('end', function () {
console.log('Stream ended')
})
You can supply an options object as the first parameter to createReadStream()
with the following properties:
gt
(greater than), gte
(greater than or equal) define the lower bound of the range to be streamed. Only entries where the key is greater than (or equal to) this option will be included in the range. When reverse=true
the order will be reversed, but the entries streamed will be the same.
lt
(less than), lte
(less than or equal) define the higher bound of the range to be streamed. Only entries where the key is less than (or equal to) this option will be included in the range. When reverse=true
the order will be reversed, but the entries streamed will be the same.
reverse
(boolean, default: false
): stream entries in reverse order. Beware that due to the way that stores like LevelDB work, a reverse seek can be slower than a forward seek.
limit
(number, default: -1
): limit the number of entries collected by this stream. This number represents a maximum number of entries and may not be reached if you get to the end of the range first. A value of -1
means there is no limit. When reverse=true
the entries with the highest keys will be returned instead of the lowest keys.
keys
(boolean, default: true
): whether the results should contain keys. If set to true
and values
set to false
then results will simply be keys, rather than objects with a key
property. Used internally by the createKeyStream()
method.
values
(boolean, default: true
): whether the results should contain values. If set to true
and keys
set to false
then results will simply be values, rather than objects with a value
property. Used internally by the createValueStream()
method.
Legacy options:
start
: instead use gte
end
: instead use lte
db.createKeyStream([options])
Returns a Readable Stream of keys rather than key-value pairs. Use the same options as described for createReadStream
to control the range and direction.
You can also obtain this stream by passing an options object to createReadStream()
with keys
set to true
and values
set to false
. The result is equivalent; both streams operate in object mode.
db.createKeyStream()
.on('data', function (data) {
console.log('key=', data)
})
// same as:
db.createReadStream({ keys: true, values: false })
.on('data', function (data) {
console.log('key=', data)
})
db.createValueStream([options])
Returns a Readable Stream of values rather than key-value pairs. Use the same options as described for createReadStream
to control the range and direction.
You can also obtain this stream by passing an options object to createReadStream()
with values
set to true
and keys
set to false
. The result is equivalent; both streams operate in object mode.
db.createValueStream()
.on('data', function (data) {
console.log('value=', data)
})
// same as:
db.createReadStream({ keys: false, values: true })
.on('data', function (data) {
console.log('value=', data)
})
db.iterator([options])
Returns an abstract-leveldown
iterator, which is what powers the readable streams above. Options are the same as the range options of createReadStream()
and are passed to the underlying store.
db.createWriteStream
?db.createWriteStream()
has been removed in order to provide a smaller and more maintainable core. It primarily existed to create symmetry with db.createReadStream()
but through much discussion, removing it was the best course of action.
The main driver for this was performance. While db.createReadStream()
performs well under most use cases, db.createWriteStream()
was highly dependent on the application keys and values. Thus we can't provide a standard implementation and encourage more write-stream
implementations to be created to solve the broad spectrum of use cases.
Check out the implementations that the community has already produced here.
levelup
ships with native Promise
support out of the box.
Each function accepting a callback returns a promise if the callback is omitted. This applies for:
db.get(key[, options])
db.put(key, value[, options])
db.del(key[, options])
db.batch(ops[, options])
db.batch().write()
The only exception is the levelup
constructor itself, which if no callback is passed will lazily open the underlying store in the background.
Example:
var db = levelup(leveldown('./my-db'))
db.put('foo', 'bar')
.then(function () { return db.get('foo') })
.then(function (value) { console.log(value) })
.catch(function (err) { console.error(err) })
Or using async/await
:
const main = async () => {
const db = levelup(leveldown('./my-db'))
await db.put('foo', 'bar')
console.log(await db.get('foo'))
}
levelup
is an EventEmitter
and emits the following events.
Event | Description | Arguments |
---|---|---|
put | Key has been updated | key, value (any) |
del | Key has been deleted | key (any) |
batch | Batch has executed | operations (array) |
opening | Underlying store is opening | - |
open | Store has opened | - |
ready | Alias of open | - |
closing | Store is closing | - |
closed | Store has closed. | - |
For example you can do:
db.on('put', function (key, value) {
console.log('inserted', { key, value })
})
A list of Level modules and projects can be found in the wiki. We are in the process of moving all this to awesome
.
Stores like LevelDB are thread-safe but they are not suitable for accessing with multiple processes. You should only ever have a store open from a single Node.js process. Node.js clusters are made up of multiple processes so a levelup
instance cannot be shared between them either.
See the aformentioned wiki for modules like multilevel, that may help if you require a single store to be shared across processes.
There are multiple ways you can find help in using Level in Node.js:
levelup
users in the ##leveldb channel on Freenode, including most of the contributors to this project.levelup
is an OPEN Open Source Project. This means that:
Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.
See the contribution guide for more details.
Cross-browser Testing Platform and Open Source ♥ Provided by Sauce Labs.
MIT © 2012-present levelup
Contributors.
[3.1.1] - 2018-07-14
airtap
from 0.0.7
to 0.1.0
(@vweevers, @ralphtheninja)level-iterator-stream
from ~2.0.0
to ~3.0.0
(@ralphtheninja)batch.write()
(@ralphtheninja)nyc
and coveralls
(@ralphtheninja)airtap --local
(@ralphtheninja)rs.destroy()
without using .bind()
(@ralphtheninja)FAQs
Fast & simple storage - a Node.js-style LevelDB wrapper
The npm package levelup receives a total of 130,718 weekly downloads. As such, levelup popularity was classified as popular.
We found that levelup demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.